/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bo;

import data.Deptposweightage;
import data.Departments;
import data.Desiglayer;
import data.DeptposweightageId;
import dao.DeptposweightageDAO;
import dao.DesignLayerDAO;
import javax.swing.ComboBoxModel;
import javax.swing.table.TableModel;
import tableview.DeptposweightageTable;

/**
 *
 * @author ChiHieu
 */
public class DeptposweightageBO {

	private String infomation;
	private DeptposweightageDAO depDAO;
	private DesignLayerDAO deslDAO;

	public DeptposweightageBO() {
		depDAO = new DeptposweightageDAO();
		deslDAO = new DesignLayerDAO();
	}

	public String getInfomation() {
		return infomation;
	}

	public void setInfomation(String infomation) {
		this.infomation = infomation;
	}

	public boolean actionCreate(String deptname, String layer, String weight) {
		if ("-----None-----".equals(deptname) || "-----None-----".equals(layer) || !isNumeric(weight)) {
			setInfomation("Please check your infomation!");
			return false;
		} else {
			Deptposweightage depw = new Deptposweightage();
			Departments dept = new Departments();
			Desiglayer desl = new Desiglayer();
			DeptposweightageId depwid = new DeptposweightageId();
			dept = depDAO.getDepartmentByName(deptname);
			desl = deslDAO.getDesignLayerByIdLayer(Integer.parseInt(layer));
			depwid.setDeptno(dept.getDeptno());
			depwid.setLayer(desl.getLayer());
			depw.setDepartments(dept);
			depw.setDesiglayer(desl);
			depw.setId(depwid);
			depw.setWeightage(Integer.parseInt(weight));
			return depDAO.executeInsertHQLQuery(depw);
		}
	}

	public ComboBoxModel getComboBox() {
		return new dao.DepartmentDAO().getComboBoxModel();
	}

	public TableModel actionShowDeptposweightage() {
		return new DeptposweightageDAO().ShowTableDeptposweightage();
	}

	public boolean isNumeric(String s) {
		return s.matches("[-+]?\\d*\\.?\\d+");
	}

	public boolean actionUpdate(String deptname, String layer, String weight) {
		if (!isNumeric(weight)) {
			setInfomation("Please check your infomation!");
			return false;
		} else {
			Deptposweightage depw = new Deptposweightage();
			Departments dept = new Departments();
			Desiglayer desl = new Desiglayer();
			DeptposweightageId depwid = new DeptposweightageId();
			dept = depDAO.getDepartmentByName(deptname);
			desl = deslDAO.getDesignLayerByIdLayer(Integer.parseInt(layer));
			depwid.setDeptno(dept.getDeptno());
			depwid.setLayer(desl.getLayer());
			depw.setDepartments(dept);
			depw.setDesiglayer(desl);
			depw.setId(depwid);
			depw.setWeightage(Integer.parseInt(weight));
			return depDAO.executeUpdateHQLQuery(depw);
		}

	}

	public boolean actionDelete(String deptname, String layer, String weight) {
			Deptposweightage depw = new Deptposweightage();
			Departments dept = new Departments();
			Desiglayer desl = new Desiglayer();
			DeptposweightageId depwid = new DeptposweightageId();
			dept = depDAO.getDepartmentByName(deptname);
			desl = deslDAO.getDesignLayerByIdLayer(Integer.parseInt(layer));
			depwid.setDeptno(dept.getDeptno());
			depwid.setLayer(desl.getLayer());
			depw.setDepartments(dept);
			depw.setDesiglayer(desl);
			depw.setId(depwid);
			depw.setWeightage(Integer.parseInt(weight));
			return depDAO.executeDeleteHQLQuery(depw);
	}
}
